﻿var template = '<span style="color:{0};">{1}</span>';

var change = function (value) {
    return Ext.String.format(template, (value > 0) ? "green" : "red", value);
};
var template2 = '<span style="color:#fff;background-color:{0};padding: 3px 10px;">{1}</span>';
var changesate = function (value) {
    return Ext.String.format(template2, (value == '已审') ? "red" : "green", value);
};
var pctChange = function (value) {
    return Ext.String.format(template, (value > 0) ? "green" : "red", (value * 100).toString().substring(0,4) + "%");
};

var shuliang = function (value) {
    return Ext.util.Format.number(value, '000,000');
};
var paymoney = function (value) {
    return Ext.util.Format.number(value, '￥000,000.00');
};
var price = function (value) {
    return Ext.util.Format.number(value, '000,000.00');
};
var date = function (value) {
    //return Ext.util.Format.DateRenderer(value, 'yyyy-MM-dd');
    return Ext.util.Format.date(value, 'Y-m-d H:i:s')
};
var date2 = function (value) {
    //return Ext.util.Format.DateRenderer(value, 'yyyy-MM-dd');
    return Ext.util.Format.date(value, 'Y-m-d')
};     
//导出EXCEL
var exportData = function (format) {
    App.FormatType.setValue(format);
    var store = App.GridPanel1.store;
    store.submitData(null, { isUpload: true });
};
var doSort = function () {
    App.Store1.sort(getSorters());
};

var getSorters = function () {
    var sorters = [];

    Ext.each(App.Toolbar2.query('button'), function (button) {
        sorters.push(button.sortData);
    }, this);

    return sorters;
};

var changeSortDirection = function (button, changeDirection) {
    var sortData = button.sortData,
                iconCls = button.iconCls;

    if (sortData) {
        if (changeDirection !== false) {
            button.sortData.direction = Ext.String.toggle(button.sortData.direction, "ASC", "DESC");
            button.setIconCls(Ext.String.toggle(iconCls, "icon-sortascending", "icon-sortdescending"));
        }
        App.Store1.clearFilter();
        doSort();
    }
};

var createItem = function (data) {
    var header = data.header,
                headerCt = header.ownerCt,
                reorderer = headerCt.reorderer;

    // Hide the drop indicators of the standard HeaderDropZone
    // in case user had a pending valid drop in 
    if (reorderer) {
        reorderer.dropZone.invalidateDrop();
    }
    return createSorterButtonConfig({
        text: header.text,
        sortData: {
            property: header.dataIndex,
            direction: "s"
        }
    });
};

var createSorterButtonConfig = function (config) {
    config = config || {};

    Ext.applyIf(config, {
        listeners: {
            click: function (button, e) {
                changeSortDirection(button, true);
            }
        },
        iconCls: config.sortData.direction.toLowerCase() == "asc" ? "icon-sortascending" : "icon-sortdescending",
        reorderable: true,
        xtype: 'splitbutton',
        menu: {
            xtype: "menu",
            items: [
                        {
                            text: "移除",
                            handler: remove
                        }
                    ]
        }
    });

    return config;
};

var canDrop = function (dragSource, event, data) {
    var sorters = getSorters(),
                header = data.header,
                length = sorters.length,
                entryIndex = this.calculateEntryIndex(event),
                targetItem = this.toolbar.getComponent(entryIndex),
                i;

    // Group columns have no dataIndex and therefore cannot be sorted
    // If target isn't reorderable it could not be replaced
    if (!header.dataIndex || (targetItem && targetItem.reorderable === false)) {
        return false;
    }

    for (i = 0; i < length; i++) {
        if (sorters[i].property == header.dataIndex) {
            return false;
        }
    }

    return true;
};

var remove = function (menu) {
    App.Toolbar2.remove(menu.up('button'));
    App.Store1.clearFilter();
    doSort();
};

 //公用下载Grid到Execl方法
function downloadViewData(grid) {
    try {
        var xls = new ActiveXObject("Excel.Application");
    }
    catch (e) {
        alert("要打印该表，您必须安装Excel电子表格软件，同时浏览器须使用“ActiveX 控件”，您的浏览器须允许执行控件。 请点击【帮助】了解浏览器设置方法！");
        return "";
    }
    xls.visible = true; //设置excel为可见    
    var xlBook = xls.Workbooks.Add;
    var xlSheet = xlBook.Worksheets(1);
    xlSheet.name = grid.title;
    var cm = grid.getColumnModel();
    var colCount = cm.getColumnCount();
    var temp_obj = [];
    //只下载没有隐藏的列(isHidden()为true表示隐藏,其他都为显示)    
    //临时数组,存放所有当前显示列的下标    
    for (i = 0; i < colCount; i++) {
        if (cm.getColumnHeader(i) == "" || cm.isHidden(i) == true) {   //不下载CheckBox列和隐藏列    
        } else {
            temp_obj.push(i);
        }
    }
    for (i = 1; i <= temp_obj.length; i++) {
        //显示列的列标题    
        xlSheet.Cells(1, i).Value = cm.getColumnHeader(temp_obj[i - 1]);
        xlSheet.Cells(1, i).Interior.ColorIndex = 15;
        xlSheet.Cells(1, i).Font.Bold = true;
        //设置导出的列格式，解决字符串在导出到excel中变成数值型    
        var fld = grid.getStore().recordType.prototype.fields.get(cm.getDataIndex(i - 1));
        if (fld.type.type == "auto") {
            xlSheet.Columns(i).NumberFormatLocal = "@";
        }
    }
    var store = grid.getStore();
    var recordCount = store.getCount();
    var view = grid.getView();
    for (i = 1; i <= recordCount; i++) {
        for (j = 1; j <= temp_obj.length; j++) {
            //EXCEL数据从第二行开始,故row = i + 1;    
            xlSheet.Cells(i + 1, j).Value = view.getCell(i - 1, temp_obj[j - 1]).innerText;
        }
    }
    xlSheet.Columns.AutoFit;
    xls.ActiveWindow.Zoom = 100
    xls.UserControl = true; //很重要,不能省略,不然会出问题 意思是excel交由用户控制    
    xls = null;
    xlBook = null;
    xlSheet = null;
}
var uploadError = function (item, file, errorCode, message) {
    alert("1Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
};

var fileSelectionError = function (item, file, errorCode, message) {
    alert("2Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
};
